package cn.monkey.family.server.domain.uc.role.datascope;

import cn.monkey.family.data.crud.repository.CrudRepository;
import cn.monkey.family.data.uc.role.datascope.DataScopeDefinition;
import cn.monkey.family.data.uc.role.datascope.RoleDataScopes;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.ListQueryByExampleExecutor;

import java.util.Collection;
import java.util.Collections;

@NoRepositoryBean
public interface RoleDataScopeRelRepository extends CrudRepository<RoleDataScopes,String>, ListQueryByExampleExecutor<RoleDataScopes> {

    default Collection<DataScopeDefinition> findByRoleId(String roleId){
        RoleDataScopes roleDataScopes = new RoleDataScopes();
        roleDataScopes.setRoleId(roleId);
        Example<RoleDataScopes> example = Example.of(roleDataScopes, ExampleMatcher.matching().withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact()));
        return this.findOne(example)
                .map(RoleDataScopes::getDataScopes)
                .orElse(Collections.emptyList());
    }
}
